cmake_minimum_required(VERSION 3.5)
#Set Project Name
project(kylin-printer LANGUAGES CXX C)
#message(${PROJECT_NAME})

option(TEST_DEBUG "option for debug" OFF)

if (TEST_DEBUG) 
	add_definitions(-DTEST_DEBUG)
    add_definitions(-DPROJECT_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}\")
    add_definitions(-DPROJECT_BINARY_DIR=\"${PROJECT_BINARY_DIR}\")
endif(TEST_DEBUG)

add_subdirectory(backend)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
find_package(PkgConfig REQUIRED)
pkg_check_modules(KYSDKQTWIDGETS_PKG kysdk-qtwidgets)
pkg_check_modules(KYSDKKABASE_PKG kysdk-kabase)
pkg_check_modules(KYSDKQTWIDGETS_PKG kysdk-qtwidgets)
pkg_search_module(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets LinguistTools X11Extras Network DBus REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets LinguistTools X11Extras Network DBus REQUIRED)
find_package(KF5WindowSystem REQUIRED)
execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/translations/generate_translations_ts.sh
                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/translations/generate_translations_qm.sh
                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})

#Gsetting
find_package(PkgConfig REQUIRED)
pkg_check_modules(QGSETTINGS REQUIRED IMPORTED_TARGET gsettings-qt)


# add ukui log
file(GLOB_RECURSE UKUI_LOG4QT "/usr/lib/libukui-log4qt.so")

#add include 
include_directories(${PROJECT_SOURCE_DIR}/printer_manager/)
include_directories(${PROJECT_SOURCE_DIR}/device_manager/)
include_directories(${PROJECT_SOURCE_DIR}/ui/)
include_directories(${PROJECT_SOURCE_DIR}/ui/custom_ui/)
include_directories(${PROJECT_SOURCE_DIR}/ui/main_win_ui/)
include_directories(${PROJECT_SOURCE_DIR}/util)
include_directories(${PROJECT_SOURCE_DIR}/dictionary)
include_directories(${PROJECT_SOURCE_DIR}/common)
include_directories(${PROJECT_SOURCE_DIR}/avahi_qt/)
include_directories(${PROJECT_SOURCE_DIR}/qt_zeroconf/)
include_directories(${PROJECT_SOURCE_DIR}/ui/new_property_window/)
include_directories(${PROJECT_SOURCE_DIR}/ui/new_property_window/SecondComponent/)
add_definitions("-Wall")

file(GLOB common_SRC 
    "common/*"
    )

file(GLOB avahi_qt_SRC 
    "avahi_qt/*.cpp"
    "avahi_qt/*.h"
    "qt_zeroconf/*.cpp"
    "qt_zeroconf/*.h"
    )

file(GLOB printer_manager_SRC 
    "printer_manager/*.cpp"
    "printer_manager/*.c"
    "printer_manager/*.h"
    )

file(GLOB device_manager_SRC 
    "device_manager/device_information.*"
    "device_manager/device_monitor.*"
    "device_manager/network_device_detector.*"
    "device_manager/network_device_monitor.*"
    "device_manager/udev_devices.*"
    )

file(GLOB dictionary_SRC
    "dictionary/*.cpp"
    "dictionary/*.h"
)

file(GLOB ui_SRC 
    "ui/*.cpp"
    "ui/*.h"
    "ui/main_win_ui/*.cpp"
    "ui/main_win_ui/*.h"
    "ui/custom_ui/*"
    "ui/new_property_window/*.h"
    "ui/new_property_window/*.cpp"
    "ui/new_property_window/SecondComponent/*.h"
    "ui/new_property_window/SecondComponent/*.cpp"
    )

file(GLOB util_SRC
    "util/*.cpp"
    "util/*.h"
    )

SET(ui_RESOURCE ${PROJECT_SOURCE_DIR}/ui/picture.qrc)

set(TS_FILES ${PROJECT_SOURCE_DIR}/translations/kylin-printer_zh_CN.ts
             ${PROJECT_SOURCE_DIR}/translations/kylin-printer_en.ts
             ${PROJECT_SOURCE_DIR}/translations/kylin-printer_dict_zh_CN.ts)
#file (GLOB TS_FILES ${PROJECT_SOURCE_DIR}/translations/zh_CN.ts ${PROJECT_SOURCE_DIR}/translations/en.ts)

QT5_CREATE_TRANSLATION(QM_FILES
    ${ui_SRC}
    ${TS_FILES})

add_executable(kylin-printer
    main.cpp
    ${common_SRC}
    ${printer_manager_SRC}
    ${device_manager_SRC}
    ${ui_SRC}
    ${util_SRC}
    ${ui_RESOURCE}
    ${avahi_qt_SRC}
    ${dictionary_SRC}
    ${QM_FILES}
)

target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets ${KYSDKQTWIDGETS_PKG_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKKABASE_PKG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKKABASE_PKG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME}  ${KYSDKKABASE_PKG_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets ${KYSDKQTWIDGETS_PKG_LIBRARIES})

target_link_libraries(kylin-printer Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::X11Extras Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::DBus)

#dependencies
target_link_libraries(kylin-printer "-lpthread -lz")
#cups
target_link_libraries(kylin-printer "-lcups")
#cupsfilter
target_link_libraries(kylin-printer "-lcupsfilters")
#deb install
target_link_libraries(kylin-printer KF5::WindowSystem)

# avahi
target_link_libraries(kylin-printer "-lavahi-common -lavahi-client")

# snmp
find_library(NETSNMPAGENT "netsnmpagent")
find_library(NETSNMPMIBS "netsnmpmibs")
find_library(NETSNMP "netsnmp")

# -lusb-1.0
target_link_libraries(${PROJECT_NAME} "-lusb-1.0")

target_link_libraries(${PROJECT_NAME} "-ludev")
target_link_libraries(${PROJECT_NAME} "-lglib-2.0")

target_link_libraries(kylin-printer ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP})

target_link_libraries(kylin-printer -lX11)
target_link_libraries(kylin-printer ${UKUI_LOG4QT})
target_link_libraries(kylin-printer PkgConfig::QGSETTINGS)
# add_custom_command(TARGET kylin-printer POST_BUILD
#     COMMAND cp ${CMAKE_BINARY_DIR}/*.qm ${PROJECT_SOURCE_DIR}/translations/)
########### install ###############
install(TARGETS kylin-printer DESTINATION /usr/bin/)
install(FILES ${PROJECT_SOURCE_DIR}/kylin-printer.desktop DESTINATION /usr/share/applications/)
install(FILES ${PROJECT_SOURCE_DIR}/data/org.ukui.log4qt.kylin-printer.gschema.xml DESTINATION /usr/share/glib-2.0/schemas/)
install(FILES ${PROJECT_SOURCE_DIR}/data/org.kylin.printer.gschema.xml DESTINATION /usr/share/glib-2.0/schemas/)

file(GLOB TRANS "${PROJECT_SOURCE_DIR}/translations/*.qm")

install(FILES ${TRANS} DESTINATION  /usr/share/kylin-printer/translations/)

# v10 SP1 only
install(DIRECTORY ${PROJECT_SOURCE_DIR}/data/guide/kylin-printer DESTINATION /usr/share/kylin-user-guide/data/guide/)
